package com.aaa.sbms.service;

import com.aaa.sbms.dao.PromotionDao;
import com.aaa.sbms.util.ResultUtil;
import com.aaa.sbms.util.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PromotionServiceImpl implements PromotionService {

    @Autowired(required = false)
    private PromotionDao proDao;

    @Override
    public Map depts() {
        Map resultMap = new HashMap();
        List<Map> list = proDao.depts();
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map positions() {
        Map resultMap = new HashMap();
        List<Map> list = proDao.positions();
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map proPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = proDao.proPage(map);
        int total = proDao.proPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map proHistoryPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = proDao.proHistoryPage(map);
        int total = proDao.proHistroryPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map proCheckPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = proDao.proCheckPage(map);
        int total = proDao.proCheckPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map proRecordPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = proDao.proRecordPage(map);
        int total = proDao.proRecordPageCount(map);
        Map resultMap= new HashMap();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map proOne(int id) {
        Map resultMap = new HashMap();
        Map list = proDao.proOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public Map proCheckOne(int id) {
        Map resultMap = new HashMap();
        Map list = proDao.proCheckOne(id);
        resultMap.put("list",list);
        return resultMap;
    }

    @Override
    public int addProAndHistory(Map map) {
        proDao.addPro(map);
        int positionid = Integer.valueOf(map.get("positionid").toString());
        int deptid = Integer.valueOf(map.get("deptid").toString());
        String dname = proDao.dnameByDeptid(deptid);
        String pname = proDao.pnameByPositionid(positionid);
        map.put("dname",dname);
        map.put("pname",pname);
        proDao.addProHistory(map);
        return 1;
    }

    @Override
    public int addProCheck(Map map) {
        Map user = (Map) UserSession.getUser();
        String phone = user.get("telephone").toString();
        int userid = Integer.valueOf(user.get("userid").toString());
        String username = user.get("username").toString();
        String sex = user.get("sex").toString();
        map.put("userid",userid);
        map.put("username",username);
        map.put("sex",sex);
        map.put("phone",phone);
        String dname = map.get("dname").toString();
        String pname = map.get("pname").toString();
        int deptid = proDao.deptidByDname(dname);
        int pid = proDao.getIdbyPname(pname);
        map.put("deptid",deptid);
        map.put("pid",pid);
        return proDao.addProCheck(map);
    }

    @Override
    public int updatePro(Map map) {
        return proDao.updatePro(map);
    }

    @Override//这里需要使用事务，要么都成功，要么都不成功
    @Transactional
    public ResultUtil yes(Map map) {
        try {
            int id = Integer.valueOf(map.get("promotionid").toString());
            Map proOne = proDao.proOne(id);
            int number = Integer.valueOf(proOne.get("number").toString());
            if (number>0){//判断该职位还缺不缺人（晋升机会）
                int resultid = Integer.valueOf(map.get("result").toString());
                if (resultid==1){//审核通过
                    proDao.updateProOfNumber(id);//机会-1
                    String pname = map.get("pname").toString();
                    int pid = proDao.getIdbyPname(pname);
                    int userid = Integer.valueOf(map.get("userid").toString());
                    proDao.updateUserOneOfPid(pid, userid);//晋升职位
                }
                proDao.addProRecord(map);//添加晋升审核历史记录
                proDao.deletePro(Integer.valueOf(map.get("id").toString()));//删除晋升申请记录
                return new ResultUtil(2001,"审核业务顺利完成",null);
            }else {
                return new ResultUtil(2002,"该职位已经不需要人了",null);
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            return new ResultUtil(2002,"捕捉到异常",null);
        }
    }
}
